var salt = sessionStorage.getItem('usersalt');
var id = getQueryVariable("id");
console.log(id)

var app = new Vue({
    el: ".layui-form",
    data: {
        list: [],
    },
    beforeCreate: function () {

    },
    updated: function () {
        layui.use(['form'], function () {
            var form = layui.form
            form.render();
        })
    }
})

layui.use(['form', 'layer', 'laypage'], function () {
    var form = layui.form, laypage = layui.laypage,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    if (!sessionStorage.getItem('username')) {
        layer.msg('请先登录');
        setTimeout("top.location.href='../../../index.html'", 1000);
    }

    getData();
    $.post(url + 'question/getTypeName', {Token: salt}, function (data) {
        if (data.code == 0) {
            var list = data.data;
            var opt = '<option value="">请选择</option>'
            for (var i = 0; i < list.length; i++) {
                opt += '<option value="' + list[i].id + '" >' + list[i].name + '</option>'
            }
            $('#typeName').append(opt);
            form.render();
        } else {
            alert(data.msg);
        }
    })
    $.post(url + 'question/getDifferServ', {Token: salt}, function (data) {
        if (data.code == 0) {
            var list = data.data;
            var opt = '<option value="">请选择</option>'
            for (var i = 0; i < list.length; i++) {
                opt += '<option value="' + list[i].id + '" >' + list[i].name + '</option>'
            }
            $('#differServ').append(opt);
            form.render();
        } else {
            alert(data.msg);
        }
    });
    form.on('checkbox', function (obj) {
        var data = $(obj.elem);
        var id = data.attr("id");
        var questiontype = data.attr("questiontype");
        if (!id) {
            var checkBox = data.parents(".subject_box").find(".subject input[type='checkbox']");
            checkBox.prop("checked", data.prop("checked"));
            checkBox.each(function () {
                if (data.prop("checked")) {
                    if (choiceArr.indexOf($(this).attr('id')) == -1) {
                        choiceArr.push($(this).attr('id'));
                        typeArr[$(this).attr('questiontype')]++
                    }
                } else {
                    choiceArr.remove($(this).attr('id'));
                    typeArr[$(this).attr('questiontype')]--
                }
            })
            form.render('checkbox');
        } else {
            data.prop("checked") ? choiceArr.push(id) : choiceArr.remove(id);
            data.prop("checked") ? typeArr[questiontype]++ : typeArr[questiontype]--
        }
        $(".choice_box .choice_num span").text(choiceArr.length);
    });
});

function getData(page) {
    var sendData = {
        limit: 10,
        id:id,
        Token: salt
    }

    // 获取数据
    $.post(url + 'questionPaper/getInfo', sendData, function (data) {
        if (data.code == 0) {
            layui.use(['laypage'], function () {
                var laypage = layui.laypage;
                laypage.render({
                    elem: 'demo0'
                    , count: data.count,
                    jump: function (obj, first) {
                        if (!first) {
                            getData(obj.curr)
                        }
                    }
                });
            })
            initQuestion(data.data);
        } else {
            alert(data.msg);
        }
    })
}


$(".add_btn").click(function () {
    location.href = "../add/add.html"
})


$("body").on("click", "#seleCheck", function () {
    getData()
});


function initQuestion(data) {
    // $(".question_name").text(data.title);
    console.log(data);
    var questionList = [];
    //题目排序 0:单选，1：多选，2：判断
    sortType = data.sortType.split("—");
    sortType.splice(sortType.indexOf("单选"), 1, "radioQuestion");
    sortType.splice(sortType.indexOf("多选"), 1, "multiSelectQuestion");
    sortType.splice(sortType.indexOf("判断"), 1, "tfngQuestion");
    sortType.forEach(function (item) {
        data[item].forEach(function (list, index) {
            list.options = JSON.parse(list.options);
            questionList.push(list);
        })
    })
    app.list = questionList;
    // resetTime(data.duration*60);
    console.log(questionList);
}







